﻿package rd.hardcore.ui.pad 
{
	import aze.motion.eaze;
	import aze.motion.EazeTween;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	import fr.digitas.flowearth.conf.Conf;
	import fr.digitas.flowearth.event.BoolEvent;
	import rd.debug.Debug;
	import rd.hardcore.datas.Track;
	import rd.hardcore.engine.datas.DatasManager;
	import rd.hardcore.engine.save.SaveManager;
	import rd.hardcore.engine.sound.HardcoreGhettoSoundSystem;
	import rd.hardcore.events.OutDragInEvent;
	import rd.hardcore.events.SoundSystemEvent;
	import rd.hardcore.events.TrackEvent;
	import rd.hardcore.params.GlobalEvents;
	import rd.hardcore.params.GlobalParams;
	
	/**
	 * Pad
	 * @author Romain Prache
	 */
	
	public class Pad extends MovieClip
	{
		[Embed(source='../../../../../bin/medias/deadHead.png')]
		private var DeadHead		: Class;
		
		/* VAR */
		public var front			: FrontPad;
		
		public var keyId			: int;
		public var key				: int;
		public var pos				: int;
		public var hit				: int;
		
		protected var _track		: Track;
		protected var streamable	: Boolean;

		private var display			: Rectangle;
		private var isDown			: Boolean;
		private var shadow			: DropShadowFilter;
		
		//	indique si le pad est en court de drag
		private var dragToKill		: Boolean	=	false;
		private var bmp				: Bitmap;
		private var timer			: uint;
		private var bmpOffsetX		: Number;
		private var bmpOffsetY		: Number;
		
		
		/* Pad */
		public function Pad() 
		{	
			display = new Rectangle(0, 0, GlobalParams.PAD_SIZE, GlobalParams.PAD_SIZE);
			scrollRect = display;
			
			front = new FrontPad();
			front.addEventListener(OutDragInEvent.DRAG_OVER, drag_overHandler);
			front.addEventListener(OutDragInEvent.DRAG_OUT, drag_outHandler);
			addChild(front);
			
			front.addEventListener(MouseEvent.CLICK, handleClick);
			front.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			
			shadow = new DropShadowFilter(2, 90, 0, .8, 5, 5, 1, 3);
			filters = [shadow];
			
			var _mask : PadMask = new PadMask();
			_mask.height = _mask.width = Conf.getNumber("sizePad");
			addChild(_mask);
			
			front.mask = _mask;
		}
		
		protected function handleMouseDown(e:MouseEvent):void 
		{
			if (track.file != "medias/songs/empty.mp3")
			{
				bmpOffsetX = mouseX * .8;
				bmpOffsetY = mouseY * .8;
				timer = setTimeout(activeDrag, 150);
			}			
		}
		
		private function activeDrag():void
		{
			dragToKill = true;
			stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMove);
			bmp = new Bitmap();
			var bmpd:BitmapData = new BitmapData(this.width * .8, this.height * .8, true, 0x000000);
			bmpd.draw(this, new Matrix(.8, 0, 0, .8),null, null, null, true);
			var deadHead:Bitmap = new DeadHead();
			bmpd.draw(deadHead,new Matrix(.8, 0, 0, .8,3), null, BlendMode.ERASE, null, true);
			bmp.bitmapData = bmpd;
			
			
			stage.addChild(bmp);
			bmp.alpha = .7;
			bmp.x = stage.mouseX - bmpOffsetX;
			bmp.y = stage.mouseY - bmpOffsetY;
			
		}
		
		private function handleMove(e:MouseEvent):void 
		{
			bmp.x = stage.mouseX - bmpOffsetX;
			bmp.y = stage.mouseY - bmpOffsetY;
		}
		
		private function handleMouseUp(e:MouseEvent):void 
		{
			dragToKill = false;
			stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleMove);
			
			var bmpRect:Rectangle = new Rectangle(bmp.x, bmp.y, bmp.width, bmp.height);
			if (Trash.zoneRect.intersects(bmpRect))
			{
				track.file = "medias/songs/empty.mp3";
				track.thumb = "medias/thumbs/empty.jpg";
				track.cpt = 0;
				SaveManager.updateTracksXML();
				SaveManager.updatePadsXML();
				track.updateSoRefresh();
				eaze(bmp).to(.4, { x:bmp.x + bmpOffsetX*.2 + bmp.width*.25, y:bmp.y + bmpOffsetY*.2 + bmp.height * .25, scaleX:.2, scaleY:.2, alpha:.2 } ).onComplete(cleanBMP);
			}else {
				eaze(bmp).to(.2, { alpha:0 } ).onComplete(cleanBMP);
			}
		}
		
		protected function cleanBMP():void
		{
			if(!dragToKill){
				if(stage.contains(bmp))	stage.removeChild(bmp);
				bmp.bitmapData.dispose();
				bmp = null;
			}
		}
		
		/* Build */
		public function build ( _node : XML) : void
		{
			keyId 	= int(	_node.@keyId.toString() );
			key		= int(	_node.toString()		);
			pos		= int(	_node.@pos.toString()	);
			hit		= int(	_node.@hit.toString()	);
			
			_track = DatasManager.getTrackById(keyId);
			_track.addEventListener(Event.CHANGE, updateTrack);
			front.setThumb(track.thumb);
			front.track = _track;
		}
		
		
		/*	Drag d'un fichier externe au dessus de la zone	*/
		protected function drag_overHandler(e:OutDragInEvent):void 
		{
			eaze(front).to(.3)
						.filter(ColorMatrixFilter, { brightness: -.5 } );
		}
		
		protected function drag_outHandler(e:OutDragInEvent):void 
		{
			eaze(front).to(.3)
						.filter(ColorMatrixFilter, { brightness: 0 } );
		}
		
		
		/* Play Sound */
		public function registerSound() : void
		{	
			if (dragToKill)	return;
			if (track.file != "medias/songs/empty.mp3") {
				dispatchEvent(	new SoundSystemEvent(SoundSystemEvent.PLAY_SOUND, track, true )	);
				track.cpt++;
				SaveManager.updatePadsXML();
				
				eaze(front).from(.3).filter(ColorMatrixFilter, { brightness: .5 } );
			}
			
			setTimeout(cleanRoll, 400);
		}
		
		protected function cleanRoll():void
		{
			front.filters = null;
		}
		
		
		/* Mouse Events */
		private function handleClick(e:MouseEvent):void
		{
			clearTimeout(timer);
			// secure
			if ( HardcoreGhettoSoundSystem.a_sndChannel.length > HardcoreGhettoSoundSystem.LIMIT) return;
			
			registerSound();
		}
		
		
		protected function updateTrack(e:Event):void 
		{
			eaze(front).to(0).filter(ColorMatrixFilter, { brightness: 0 } );
			
			front.setThumb(track.thumb);
			front.filters = null;
		}
		
		public function get track():Track { return _track; }
		
		
		
	}
	
}